package com.zp.qualitypractice.list;


import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by izp on 2017/4/22.
 */

public class CustomLoadingProgressPainter extends CustomLoadingAnimationController {
    public static final float SCALE=1.0f;

    public static final int ALPHA=255;

    float[] scaleFloats=new float[]{SCALE,
            SCALE,
            SCALE,
            SCALE,
            SCALE,
            SCALE,
            SCALE,
            SCALE};
    int[] alphas=new int[]{ALPHA,
            ALPHA,
            ALPHA,
            ALPHA,
            ALPHA,
            ALPHA,
            ALPHA,
            ALPHA};
    @Override
    public void draw(Canvas canvas, Paint paint) {
        float radius=getWidth()/10;//targetView的宽度，这里有个疑问targetView到底是整个item的宽度还是一个方形的View的宽度
        for (int i = 0; i < 8; i++) {
            //这应该是画8个圆泡泡
            canvas.save();//保存一下状态

            PointF point = createPoint(getWidth(),getHeight(),getWidth()/2-radius,i*(Math.PI/4));
//            i*(Math.PI/4)为什么就是圆上点A与X轴的夹角？，角度不应该是360么？
            canvas.translate(point.x,point.y);//平移到指定点，
            canvas.scale(scaleFloats[i],scaleFloats[i]);
            paint.setAlpha(alphas[i]);
            canvas.drawCircle(0,0,radius,paint);//画圆点
            canvas.restore();//恢复

        }
    }


    @Override
    public List<Animator> createAnimation() {
        List<Animator> animators = new ArrayList<>();
        int[] delayArray = {0,120,240,360,480,600, 720, 780, 840};
        for (int i = 0; i < 8; i++) {
            final int index=i;
            ValueAnimator scaleValueAnimator = ValueAnimator.ofFloat(1f,0.4f,1f);//值动画
            scaleValueAnimator.setDuration(1000);
            scaleValueAnimator.setRepeatCount(-1);//重复次数设置为0？？？
            scaleValueAnimator.setStartDelay(delayArray[i]);//延迟start动画
            scaleValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    scaleFloats[index] =(float)valueAnimator.getAnimatedValue();
                    postInvalidate();
                }
            });
            scaleValueAnimator.start();//前面设置的startDelay为毛不在这里设置起作用？
            ValueAnimator alphaValueAnimation;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                alphaValueAnimation = ValueAnimator.ofArgb(255,77,255);
            }else {
                alphaValueAnimation = ValueAnimator.ofInt(255,77,255);
            }
            alphaValueAnimation.setDuration(1000);
            alphaValueAnimation.setRepeatCount(-1);
            alphaValueAnimation.setStartDelay(delayArray[i]);
            alphaValueAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    alphas[index] = (int)valueAnimator.getAnimatedValue();
                    postInvalidate();
                }
            });
            alphaValueAnimation.start();
            animators.add(scaleValueAnimator);
            animators.add(alphaValueAnimation);
        }

        return animators;
    }

    /**
     * 圆O的圆心为(a,b),半径为R,点A与到X轴的为角α.
     *则点A的坐标为(a+R*cosα,b+R*sinα)
     * 围绕一个圆画点，这些点平均八个方向
     * @param width
     * @param height
     * @param radius
     * @param angle
     * @return
     */
    private PointF createPoint(int width,int height,float radius,double angle){
        float x = (float)(width/2+radius*(Math.cos(angle)));
        float y = (float)(height/2+radius*(Math.sin(angle)));

        return new PointF(x,y);
//        return new PointF(width/8+height,width/8+height);
    }
}
